package dyy.zc.alumnibook.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.dalong.library.view.LoopRotarySwitchView;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.base.BaseActivity;
import dyy.zc.alumnibook.bean.PhotoAlbum;
import dyy.zc.alumnibook.bean.UserInfo;
import dyy.zc.alumnibook.utils.BmobUtils;
import dyy.zc.alumnibook.utils.ImageLoaderUtils;
import dyy.zc.alumnibook.utils.Utils;
import dyy.zc.alumnibook.views.EmptyView;

import static dyy.zc.alumnibook.constant.Constants.ResultCode.SUCCESS;

public class CheckPhotoActivity extends BaseActivity {
    private LoopRotarySwitchView mLoopRotarySwitchView;//旋转图片
    public int width;//设置旋转图片的宽度
    public PhotoAlbum photoAlbum;
    private TextView title;
    private BmobUtils bmobUtils;
    private EmptyView emptyView;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    Bundle data = msg.getData();
                    PhotoAlbum photo = (PhotoAlbum) data.getSerializable("list");
                    if (photo !=null){
                        photoAlbum = photo;
                        emptyView.setEmptyViewGone();
                        setData();
                    }else {
                        emptyView.setNotify("暂无图片");
                    }
                    break;
            }
        }
    };

    private void setData() {
        for (int i = 0; i < photoAlbum.getImg().size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.photo_show, null);
            ImageView image = (ImageView) view.findViewById(R.id.image);
            ImageLoaderUtils.initImage(this, photoAlbum.getImg().get(i).getFileUrl(), image, R.mipmap.logo);
            mLoopRotarySwitchView.addView(view);
        }
        initLoopRotarySwitchView();

    }

    @Override
    protected void findViewByIDS() {
        mLoopRotarySwitchView = findViewsById(R.id.loopView);
        title = findViewsById(R.id.top_tv_title);
        emptyView = new EmptyView(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_photo);
        initData();
        bind();
    }

    private void bind() {

    }

    private void initData() {
        title.setText("相册");
        bmobUtils = new BmobUtils(this);
        Intent intent = getIntent();
        if (intent.getIntExtra("id", 0) == 1) {//为1没有弹窗，为0有
            UserInfo userInfo = (UserInfo) intent.getSerializableExtra("photo");
            findViewsById(R.id.top_rl_right).setVisibility(View.GONE);
            bmobUtils.findFriendPhoto(userInfo,SUCCESS,handler);
        }else {
            bmobUtils.queryPhoto(loginuser,SUCCESS,handler);
        }
    }

    /**
     * 初始化循环图片
     */
    private void initLoopRotarySwitchView() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        mLoopRotarySwitchView
                .setR(width/2)//设置R的大小
                .setAutoRotation(true)//是否自动切换
                .setAutoRotationTime(2000);//自动切换的时间  单位毫秒
    }
}
